﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Douban.Controls
{
    public class AutoResizePanel : Panel
    {
        private double _rowHeight;
        private double _actualWidth;
        private double _scaleFactor;
        
        protected override Size MeasureOverride(Size availableSize)
        {
            double width = 0.0;
            foreach (UIElement element in base.InternalChildren)
            {
                element.Measure(availableSize);
                Size desiredSize = GetDesiredSizeWithoutMargin(element);
                _rowHeight = Math.Max(_rowHeight, desiredSize.Height);
                width += desiredSize.Width;
            }

            if (width < availableSize.Width)
            {
                _scaleFactor = 1.0;
                _actualWidth = availableSize.Width / base.InternalChildren.Count;
            }
            else if(width > availableSize.Width)
            {
                _scaleFactor = availableSize.Width / width;
                width = 0.0;
                foreach (UIElement element in base.InternalChildren)
                {
                    element.Measure(
                        new Size(
                            element.DesiredSize.Width * _scaleFactor,
                            availableSize.Height));
                    width += element.DesiredSize.Width;
                }
            }

            return new Size(width, this._rowHeight);
        }

        //TODO Squeeze tab control undone
        protected override Size ArrangeOverride(Size arrangeSize)
        {
            Point point = new Point();

            foreach (UIElement element in this.Children)
            {
                Size sizeWithoutMargin = this.GetDesiredSizeWithoutMargin(element);
                Thickness margin = (Thickness)element.GetValue(FrameworkElement.MarginProperty);
                double width = sizeWithoutMargin.Width;
                if (element.DesiredSize.Width != sizeWithoutMargin.Width)
                    width = arrangeSize.Width - point.X;

                // Last-tab-selected "fix"
                element.Arrange(
                    new Rect(point, new Size(_actualWidth * _scaleFactor, this._rowHeight)));
                double leftRightMargin = Math.Max(0.0, -(margin.Left + margin.Right));
                point.X += _actualWidth + (leftRightMargin * this._scaleFactor);
            }

            return arrangeSize;
        }

        private Size GetDesiredSizeWithoutMargin(UIElement element)
        {
            Thickness margin = (Thickness)element.GetValue(FrameworkElement.MarginProperty);
            Size desiredSize = new Size();
            desiredSize.Height = Math.Max(0.0, element.DesiredSize.Height - (margin.Top + margin.Bottom));
            desiredSize.Width = Math.Max(0.0, element.DesiredSize.Width - (margin.Left + margin.Right));

            return desiredSize;
        }
    }
}
